/*
 * gy_enpclient.h
 *
 *  Created on: 2015年9月15日
 *      Author: gaoyi
 */

#ifndef GY_ENPCLIENT_H_
#define GY_ENPCLIENT_H_

#include "gy_enpdef.h"


class GYEnpCtrl;
class GYEnpListen;
class GYEnpClient : public CTEpollObject<GYEnpCtrl>
{
protected:
	friend class GYEnpCtrl;
	friend class GYEnpClientFactory;
protected:
	GYEnpCtrl*			m_pstOwner;
	GYEnpListen*		m_pstListen;
	CTSocket			m_stSocket;
	CEnpNetHead			m_stNetHead;
	CTBuffer			m_stRecvBuf;			///< 接收缓冲区
	CTBuffer			m_stSendBuf;			///< 发送缓冲区
	int					m_iRecvBytes;			///< 接收的累计数据量
	int					m_iSendBytes;			///< 发送的累计数据量
	unsigned int		m_uiSeq;				///< 客户端序号，每个客户端连接分配一个
public:
	GYEnpClient():m_stSocket(PF_INET,SOCK_STREAM,false),
	m_iRecvBytes(0),m_iSendBytes(0),m_uiSeq(0)
	{
		m_pstOwner = NULL;
		m_pstListen = NULL;
	}

	GYEnpClient(const GYEnpClient& client):m_pstOwner(client.m_pstOwner), m_pstListen(client.m_pstListen),
			m_stSocket(client.m_stSocket),m_stNetHead(client.m_stNetHead),m_stRecvBuf(client.m_stRecvBuf),
			m_stSendBuf(client.m_stSendBuf),m_iRecvBytes(client.m_iRecvBytes),
			m_iSendBytes(client.m_iSendBytes),m_uiSeq(client.m_uiSeq)

	{

	}

	virtual ~GYEnpClient()
	{
		Unregister(); // 先Unregister从epoll注销, 然后再关闭socket
		m_pstOwner = NULL;
		m_pstListen = NULL;
	}
public:
    int Init(GYEnpCtrl& stOwner, GYEnpListen& stListen,
        SOCKET iSocket, unsigned int uiIP, unsigned short ushPort);
public:
    int	Recv();
};

#endif /* GY_ENPCLIENT_H_ */
